/**
 * name: messages.js
 * javascript dependencies: PageBus(1.2+)
 * description: Describes the application's message subject, subcription and publishing semantics, as well
 * as all of the messages published by the user interface.
 */

/**
 * Web appplication message facade.
 */
if (MessageBus == null) var MessageBus = {};

/**
 * The message bus implementation.
 */
MessageBus.messageBusProvider = window.PageBus;

/**
 * Publish a message that is managed by the client application's message bus.
 * @param subject The message subject. For example,
 * eg.customer.onSelect is an appropriate subject name, but not link.click.
 * @param message The message.
 */
MessageBus.publish = function(subject, message)
{
    // use tibco message page bus publishing method.
    MessageBus.messageBusProvider.publish(subject, message);
};

/**
 * Subcribe to a message that is managed by the client application's message bus.
 * @param subject The message subject. For example,
 * eg.customer.onSelect is an appropriate subject name, but not link.click.
 * @param scope The scope object for the callback to be invoked on.
 * @param callback The message callback.
 * @return subscription
 */
MessageBus.subscribe = function(subject, scope, callback)
{
    // use tibco message page bus subscribe method.
    return MessageBus.messageBusProvider.subscribe(subject, scope, callback, null);
};

/**
 * The unsubscribe operation cancels a subscription, using the Subscription object
 * returned by MessageBus.subscribe as a handle to the subscription.
 * @param subscription the subscription returned by MessageBus.subscribe.
 */
MessageBus.unsubscribe = function(subscription)
{
    // use tibco message page bus unsubscribe method.
    MessageBus.messageBusProvider.unsubscribe(subscription);
};

/**
 *
 * @param list
 * pop all the subscriptions off an array list and unsubscribe them
 */
MessageBus.unsubscribeList =function(list){
    while (list.length > 0)
       this.unsubscribe(list.pop());
};

// Defined Message Publishing Methods

/**
 * Public the invite user message
 * @param userName The user name.
 * @param email The user's email.
 */
MessageBus.publishCreateInvitationMessage = function(recipientId, nickname)
{
    /**
     * Message decription: name of the message and a
     * body for additional information.
     */
    
    var invitationsMessage = {
        name: "forexpert.trader.existing.inviteMessage",
        recipientId: recipientId,
        nickname: nickname
    }

    MessageBus.publish(FOREXPERT_TRADER_EXISTING_ON_INVITE, invitationsMessage);
};

/**
 * Get trader profile indetifier.
 * @param userId
 */
MessageBus.publishViewTraderProfileMessage = function(userId, nickname)
{
    var viewTraderProfileMessage = {
        name: "forexpertr.trader.profile.viewTraderProfileMessage",
        userId: userId,
        nickname: nickname
    }

    MessageBus.publish(FOREXPERT_TRADER_PROFILE_ON_SHOW, viewTraderProfileMessage);
};

/**
 * Open a "Contact Us forrm.
 * @param userEmail, functional Area
 */
MessageBus.publishViewContactUsMessage = function(functionalArea, type)
{
    var viewContactUsMessage = {
        name: "forexpert.contactUs.viewContactUsMessage",
        functionalArea: functionalArea,
        type: type
    }

    MessageBus.publish(FOREXPERT_CONTACT_US_ON_SHOW, viewContactUsMessage);
};

/**
 * Set a panel to visible.
 *
 * @param panelId
 */
MessageBus.publishShowPanelMessage = function(panelId)
{

    var showPanelMessage = {
        element: panelId
    };

    MessageBus.publish(FOREXPERT_PANEL_ON_SHOW, showPanelMessage);
};

/**
 * Notify LS connection was reconnected.
 */
MessageBus.publishLSReconnectedMessage = function()
{
    MessageBus.publish(FOREXPERT_LIGHTSTREAMER_CONN_ON_RECONNECTED);
};

/**
 * Show the New Direct Message lightbox.
 *
 * @param recipientId the message recipient's user id
 * @param nickname the message recipient's nickname
 * @param replyMessage (Opiontal) if creates a new message, this parameter is null; if replies to an existing message,
 *                     this is the text content of the existing message. 
 */
MessageBus.publishOpenMessageLightboxMessage = function(recipientId, nickname, replyMessage)
{
    var message = {
        recipientId: recipientId,
        nickname: nickname,
        replyMessage: replyMessage
    };

    MessageBus.publish(FOREXPERT_MESSAGE_LIGHTBOX_OPEN, message);
};

/**
 * Show the Economic Calendar lightbox.
 * 
 * @param calendarEvent the Economic Calendar event object
 */
MessageBus.publishOpenEconomicCalendarLightboxMessage = function(calendarEvent)
{
    var message = {
        event: calendarEvent
    };

    MessageBus.publish(FOREXPERT_ECONOMIC_CALENDAR_LIGHTBOX_OPEN, message);
};

/**
 * Economic Calendar widget updates post count for an Economic Calendar event.
 *
 * @param eventId
 * @param count
 */
MessageBus.publishEconomicEventPostCountUpdate = function(eventId, count)
{
    var message = {
        id: eventId,
        count: count
    };

    MessageBus.publish(FOREXPERT_ECONOMIC_CALENDAR_EVENT_POST_COUNT_UPDATED, message);
};

/**
 * Publishes ThumbsUp like/unlike event.
 *
 * @param isLike true if like, false if unlike.
 */
MessageBus.publishThumbsUpEvent = function(isLike)
{
    if(isLike)
        MessageBus.publish(FOREXPERT_THUMBSUP_CHANGE_LIKE);
    else
        MessageBus.publish(FOREXPERT_THUMBSUP_CHANGE_UNLIKE);
};

/**
 * Publishes browser onfocus/onblur messages.
 */
MessageBus.publishBrowserOnFocus = function()
{
    MessageBus.publish(FOREXPERT_BROWSER_ON_FOCUS);
};

MessageBus.publishBrowserOnBlur = function()
{
    MessageBus.publish(FOREXPERT_BROWSER_ON_BLUR);
};

/**
 * Publishes in-site search event.
 *
 * @param searchText The search text.
 */
MessageBus.publishDoSearchEvent = function(searchText)
{
    MessageBus.publish(FOREXPERT_SITE_SEARCH, searchText);
};

/**
 * Publishes a "show strategy" event
 *
 * @param strategyId the id of the strategy object
 */
MessageBus.publishShowStrategyEvent = function(strategyId)
{
    MessageBus.publish(FOREXPERT_STRATEGY_SHOW, strategyId);
};

